﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SMSSendTest
{
    public class UMS86SmsService
    {
        private static readonly string apiUrl = "http://api.ums86.com:8899/sms/Api/Send.do";
     

        public static string SendSmsAsync(string phoneNumber, string message)
        {
            try
            {
                using (var client = new HttpClient())
                {
                   
                 
                    string spCode = "216635",
                        loginName = "sz_sjps",
                        password = "gpsgs58226678",                     

                        TemplateCode = "2061012245588",
                        serialNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
                  
                    var strSend = new StringBuilder();
                    strSend.Append($"SpCode={spCode}&LoginName={loginName}&Password={password}&MessageContent={message}&UserNumber={phoneNumber}&SerialNumber={serialNumber}&templateId={TemplateCode}&ScheduleTime=&ExtendAccessNum=&f=1 ");
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    byte[] bData = Encoding.GetEncoding("GBK").GetBytes(strSend.ToString());
                    HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
                    hwRequest.Method = "POST";
                    hwRequest.ContentType = "application/x-www-form-urlencoded";
                    hwRequest.ContentLength = bData.Length;

                    Stream swWrite = hwRequest.GetRequestStream();
                    swWrite.Write(bData, 0, bData.Length);
                    swWrite.Close();

                    HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                    StreamReader swReader = new StreamReader(hwResponse.GetResponseStream(),Encoding.Default);
                    string strResult = swReader.ReadToEnd();
                    
                    swReader.Close();
                    hwResponse.Close();
                    Console.WriteLine(strResult);

                    
                    return strResult; // 这里返回的可能是发送成功的消息或者短信ID等   
                }
            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");
                return null;
            }
        }

        public static string SendReportSmsAsync(string phoneNumber, string message)
        {
            try
            {
                using (var client = new HttpClient())
                {


                    string spCode = "216635",
                        loginName = "sz_sjps",
                        password = "gpsgs58226678",
                        TemplateCode = "2061012245588",
                        serialNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";

                    var strSend = new StringBuilder();
                    strSend.Append($"SpCode={spCode}&LoginName={loginName}&Password={password}&MessageContent={message}&UserNumber={phoneNumber}&SerialNumber={serialNumber}&templateId={TemplateCode}&ScheduleTime=&ExtendAccessNum=&f=1 ");
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    byte[] bData = Encoding.GetEncoding("GBK").GetBytes(strSend.ToString());
                    HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
                    hwRequest.Method = "POST";
                    hwRequest.ContentType = "application/x-www-form-urlencoded";
                    hwRequest.ContentLength = bData.Length;

                    Stream swWrite = hwRequest.GetRequestStream();
                    swWrite.Write(bData, 0, bData.Length);
                    swWrite.Close();

                    HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                    StreamReader swReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.Default);
                    string strResult = swReader.ReadToEnd();

                    swReader.Close();
                    hwResponse.Close();
                    Console.WriteLine(strResult);


                    return strResult; // 这里返回的可能是发送成功的消息或者短信ID等   
                }
            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");
                return null;
            }
        }

    }
}
